package com.lh.music.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.lh.music.PlayApplication;
import com.lh.music.constant.Constants;
import com.lh.music.dao.SongDao;

import java.sql.SQLException;

import static android.content.Context.NOTIFICATION_SERVICE;

public class PlayReceiver extends BroadcastReceiver {
    private PlayApplication app;

    int position;
    public PlayReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        app= (PlayApplication) context.getApplicationContext();
        //根据不同广播  调用不同的播放功能
        String action=intent.getAction();
        //在入口处已经绑定了服务，application中的music已经有了值
        switch (action){
            case Constants.PAUSE_ACTION:
                app.music.pausePlay();
                break;
            case Constants.CONTINUE_ACTION:
                app.music.continuePlay();
                break;
            case Constants.PLAY_ACTION:
                //app.music.continuePlay();
                app.notificationManager= (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
                //代表是停止状态  发送的播放广播
                if (intent.getIntExtra("position",-1)==-1){
                    int position=-1;
                    try {
                        app.currentSongList.addAll(new SongDao(context).findPlaySongList());
                        //找到播放的位置
                        for (int i=0;i<app.currentSongList.size();i++){
                            if (app.currentSongList.get(i).getTitle().equals(app.currentSong.getTitle())){
                                position=i;
                            }
                        }
                        app.music.initMusic(app.currentSongList,position);
                        app.music.stopPlay();
                        app.music.startPlay();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }//点击播放列表发送的广播
                else{
                    position=intent.getIntExtra("position",0);
                    //初始化音乐   更新播放位置
                    app.music.initMusic(app.currentSongList,position);
                    app.music.stopPlay();
                    app.music.startPlay();
                }
                if (app.currentSongList.size()>0){
                    app.notificationManager.notify(Constants.NOTI_ID,app.notification);
                }
                break;
            case Constants.NEXT_ACTION:
                app.music.nextPlay();
                break;
            case Constants.PREV_ACTION:
                app.music.prevPlay();
                break;
            case Constants.SEEK_ACTION:
                app.music.seekToProgress();
                break;
        }
    }


}
